package cn.tta.service.impl;

import cn.tta.common.constant.MessageConstant;
import cn.tta.common.constant.StatusConstant;
import cn.tta.common.exception.DeletionNotAllowedException;
import cn.tta.common.exception.SetmealEnableFailedException;
import cn.tta.common.result.PageResult;
import cn.tta.mapper.DishMapper;
import cn.tta.mapper.SetmealDishMapper;
import cn.tta.mapper.SetmealMapper;
import cn.tta.pojo.dto.SetmealDTO;
import cn.tta.pojo.dto.SetmealPageQueryDTO;
import cn.tta.pojo.entity.Dish;
import cn.tta.pojo.entity.Setmeal;
import cn.tta.pojo.entity.SetmealDish;
import cn.tta.pojo.vo.DishItemVO;
import cn.tta.pojo.vo.SetmealVO;
import cn.tta.service.SetmealService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if (StatusConstant.ENABLE.equals(setmeal.getStatus())) {
                // 起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(setmealId-> {
            setmealMapper.deleteById(setmealId);
            setmealDishMapper.deleteBySetmealId(setmealId);
        });
    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmean = setmealMapper.getById(id);
        List<SetmealDish> setmealDishs = setmealDishMapper.getBySetmealId(setmean.getId());
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmean,setmealVO);
        setmealVO.setSetmealDishes(setmealDishs);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        if (Objects.equals(status, StatusConstant.ENABLE)) {
            List<Dish> list = dishMapper.getBySetmealId(id);
            list.forEach(dish -> {
                if (StatusConstant.DISABLE.equals(dish.getStatus())) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal build = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(build);
    }

    @Override
    public List<Setmeal> list(Setmeal build) {
        List<Setmeal> list = setmealMapper.list(build);
        return list;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemById(id);
    }


}
